#!/bin/bash

# 生成一个所有主机相同的 zookeeper 配置文件
conf_zookeeper() {

  echo_info "conf zoo file"
  cat >/tmp/zoo.cfg <<EOF
tickTime=2000
initLimit=10
syncLimit=5

# *****  CHANGED START *****

dataDir=$HRS_ZOOKEEPER_DATADIR
dataLogDir=$HRS_ZOOKEEPER_DATALOGDIR

# the port at which the clients will connect
clientPort=$HRS_ZOOKEEPER_clientPort

maxClientCnxns=1024
EOF

  HRS_ZOOKEEPER_PORT_INCFG=${HRS_ZOOKEEPER_PORT// /:}
  server_no=0
  local value_arr=($HRS_ZOOKEEPER_HOSTS)
  for i in ${value_arr[@]}; do
    server_no=$(expr $server_no + 1 )
    echo "server.$server_no=${i}:$HRS_ZOOKEEPER_PORT_INCFG"  >> /tmp/zoo.cfg
  done
  echo "" >> /tmp/zoo.cfg
  echo "# *****  CHANGED END  *****" >> /tmp/zoo.cfg
  echo "" >> /tmp/zoo.cfg
}

# 生成一个所有主机相同的 log4j 配置文件
conf_log4j() {

  echo_info "conf logfile"
  cat >/tmp/log4j.properties <<EOF
zookeeper.root.logger=INFO, ROLLINGFILE
zookeeper.console.threshold=INFO
zookeeper.log.dir=.
zookeeper.log.file=zookeeper.log
zookeeper.log.threshold=DEBUG
zookeeper.tracelog.dir=.
zookeeper.tracelog.file=zookeeper_trace.log

#
# ZooKeeper Logging Configuration
#

# Format is "<default threshold> (, <appender>)+

log4j.rootLogger=\${zookeeper.root.logger}

#log4j.rootLogger=TRACE, ROLLINGFILE, TRACEFILE

#
# Add ROLLINGFILE to rootLogger to get log file output
#    Log DEBUG level and above messages to a log file
log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLINGFILE.Threshold=\${zookeeper.log.threshold}
log4j.appender.ROLLINGFILE.File=\${zookeeper.log.dir}/\${zookeeper.log.file}

# Max log file size of 10MB
log4j.appender.ROLLINGFILE.MaxFileSize=10MB
# limit number of backup files
log4j.appender.ROLLINGFILE.MaxBackupIndex=100
log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n


#
# Add TRACEFILE to rootLogger to get log file output
#    Log DEBUG level and above messages to a log file
log4j.appender.TRACEFILE=org.apache.log4j.FileAppender
log4j.appender.TRACEFILE.Threshold=TRACE
log4j.appender.TRACEFILE.File=\${zookeeper.tracelog.dir}/\${zookeeper.tracelog.file}

log4j.appender.TRACEFILE.layout=org.apache.log4j.PatternLayout
### Notice we are including log4j's NDC here (%x)
log4j.appender.TRACEFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L][%x] - %m%n

EOF

}

# 参数：start / stop
zookeeper_ctl()
{
  echo ""
  echo_info "Zookeeper $1 ... ..."
  local value_arr=($HRS_ZOOKEEPER_HOSTS)
  for i in ${value_arr[@]}; do
    echo_info "deal on ${i} ... ..."
    ssh -o StrictHostKeyChecking=no $i "if [ -f \"$HRS_ZOOKEEPER_HOME/bin/zkServer.sh\" ]; then $HRS_ZOOKEEPER_HOME/bin/zkServer.sh $1; fi"
  done

  # 检查进程是否已经启动
  for i in ${value_arr[@]}; do
    if [ "$1" == "start" ]; then
      waiting_proc_status $HRS_ZOOKEEPER_clientPort 0 "$i start" $i
    else
      waiting_proc_status $HRS_ZOOKEEPER_clientPort 1 "$i stop" $i
    fi
  done
  sleep 1
  echo_info "Zookeeper $1 done."
}

# 安装 zookeeper
setup_zookeeper()
{
  echo_start "$1 ... ..."
  confirm_operate "install zookeeper"
  if [ $? -eq 0 ]; then
    if [ "x$HRS_ZOOKEEPER_HOSTS" = "x" ]; then
      echo_warn "The variable HRS_ZOOKEEPER_HOSTS does not assign a value, Unable to install! "
      echo_passed "$1 SKIP"
      return 0
    fi
    # 停止zookeeper
    zookeeper_ctl "stop"
    sleep 3
    conf_zookeeper
    conf_log4j
    local zk_myid=0
    local value_arr=($HRS_ZOOKEEPER_HOSTS)
    for i in ${value_arr[@]}; do
      zk_myid=$(expr $zk_myid + 1 )
      scp $XDIR/packages/zookeeper-3.4.9.tar.gz  root@${i}:/tmp >> $SETUP_LOGFILE || { die "scp zk to [${i}] fail."; }

      ssh -o StrictHostKeyChecking=no $i "rm -rf $HRS_ZOOKEEPER_DATADIR && rm -rf $HRS_ZOOKEEPER_DATALOGDIR && rm -rf $HRS_ZOOKEEPER_RUNLOGDIR && rm -rf $HRS_ZOOKEEPER_HOME && mkdir -p $HRS_ZOOKEEPER_HOME && tar -zxf /tmp/zookeeper-3.4.9.tar.gz -C $HRS_ZOOKEEPER_HOME && mv $HRS_ZOOKEEPER_HOME/zookeeper-3.4.9/* $HRS_ZOOKEEPER_HOME && rm -rf $HRS_ZOOKEEPER_HOME/zookeeper-3.4.9 && mkdir -p $HRS_ZOOKEEPER_DATADIR && mkdir -p $HRS_ZOOKEEPER_DATALOGDIR && mkdir -p $HRS_ZOOKEEPER_RUNLOGDIR" || { die "init env on [${i}] fail."; }
      sleep 1
      scp /tmp/zoo.cfg root@${i}:$HRS_ZOOKEEPER_HOME/conf/ >> $SETUP_LOGFILE || { die "scp cfg to [${i}] fail."; }
      scp /tmp/log4j.properties root@${i}:$HRS_ZOOKEEPER_HOME/conf/ >> $SETUP_LOGFILE || { die "scp log4j to [${i}] fail."; }
      ssh -o StrictHostKeyChecking=no $i "echo $zk_myid >$HRS_ZOOKEEPER_DATADIR/myid" || { die "set myid on [${i}] fail."; }
      
      scp $XDIR/conf/zk/*.sh root@${i}:$HRS_ZOOKEEPER_HOME/bin/ >> $SETUP_LOGFILE || { die "scp zksh to [${i}] fail."; }
      #ssh -o StrictHostKeyChecking=no $i "sed -i 's%^[[:space:]]*ZOO_LOG_DIR=.*%    ZOO_LOG_DIR=\"$HRS_ZOOKEEPER_RUNLOGDIR\"%' $HRS_ZOOKEEPER_HOME/bin/zkEnv.sh" || { echo_error "[$HRS_ZOOKEEPER_HOME zkEnv] : modify ZOO_LOG_DIR fail on [$i]."; }
      ssh -o StrictHostKeyChecking=no $i "sed -i 's%HRS_ZOOKEEPER_RUNLOGDIR%$HRS_ZOOKEEPER_RUNLOGDIR%' $HRS_ZOOKEEPER_HOME/bin/zkEnv.sh" || { echo_error "zkEnv : modify ZOO_LOG_DIR fail on [$i]."; }

      # 设置环境变量
      #ssh -o StrictHostKeyChecking=no $i 'bash -s' < $XDIR/util/func_rmot_setprofile.sh "HRS\ ZOOKEEPER\ ENV" "HRSCOMMENT" "$HYREN_USER"
      ssh -o StrictHostKeyChecking=no $i 'bash -s' < $XDIR/util/func_rmot_setprofile.sh "HRS_ZOOKEEPER_HOME" "$HRS_ZOOKEEPER_HOME" "$HYREN_USER"
      ssh -o StrictHostKeyChecking=no $i 'bash -s' < $XDIR/util/func_rmot_setprofile.sh "HRS_ZOOKEEPER_DATADIR" "$HRS_ZOOKEEPER_DATADIR" "$HYREN_USER"
      ssh -o StrictHostKeyChecking=no $i 'bash -s' < $XDIR/util/func_rmot_setprofile.sh "HRS_ZOOKEEPER_DATALOGDIR" "$HRS_ZOOKEEPER_DATALOGDIR" "$HYREN_USER"
      ssh -o StrictHostKeyChecking=no $i 'bash -s' < $XDIR/util/func_rmot_setprofile.sh "HRS_ZOOKEEPER_RUNLOGDIR" "$HRS_ZOOKEEPER_RUNLOGDIR" "$HYREN_USER"
      ssh -o StrictHostKeyChecking=no $i 'bash -s' < $XDIR/util/func_rmot_setprofile.sh "HRS_ZOOKEEPER_PORT" "$HRS_ZOOKEEPER_PORT" "$HYREN_USER"
      ssh -o StrictHostKeyChecking=no $i 'bash -s' < $XDIR/util/func_rmot_setprofile.sh "HRS_ZOOKEEPER_clientPort" "$HRS_ZOOKEEPER_clientPort" "$HYREN_USER"

      echo_info "setup [$i] done."
    done
    echo_success "$1"
    sleep 3
    zookeeper_ctl "start"
    echo_success "start zookeeper"
    echo_info "****************************************"
    echo_info "ZKRUN HOME    dir  : $HRS_ZOOKEEPER_HOME"
    echo_info "ZKRUN RUN LOG dir  : $HRS_ZOOKEEPER_RUNLOGDIR"
    echo_info "ZKRUN DATA    dir  : $HRS_ZOOKEEPER_DATADIR"
    echo_info "ZKRUN DATALOG dir  : $HRS_ZOOKEEPER_DATALOGDIR"
    echo_info "ZKRUN SERVER PORT  : $HRS_ZOOKEEPER_PORT"
    echo_info "ZKRUN CLIENT PORT  : $HRS_ZOOKEEPER_clientPort"
    echo_info "****************************************"

    echo_passed "$1 done."
  else
    echo_passed "$1 SKIP"
  fi
}
